import sqlite3
import os
import requests
import json
import time
import re
import json
import subprocess

class change:
    def write(self,path,value):
        with open(path, 'w') as file:
            # 写入数据到文件
            file.write(str(value))

    def open(self,path):
        with open(path, 'r') as file:
            # 读取文件内容
            content = file.read()
        return content


    def oaid(self,device):
        r=subprocess.Popen("adb -s "+device+" shell pm list packages ",shell=True, stdout=subprocess.PIPE).stdout.read()
        text=r.decode('utf-8')
        if('com.github.gzuliyujiang.demo.oaid' not in text):
            b=False
            print('app not install')
            print(subprocess.Popen("adb -s "+device+ " install E:\\auto-myt\\app_4.2.8_dev-arm64-v8a-release-20210212.apk",shell=True, stdout=subprocess.PIPE).stdout.read())
        subprocess.Popen("adb -s "+device+" shell rm -rf /data/user/0/com.github.gzuliyujiang.demo.oaid/* ",shell=True, stdout=subprocess.PIPE).stdout.read()
        subprocess.Popen("adb -s "+device+" shell am force-stop com.github.gzuliyujiang.demo.oaid ",shell=True, stdout=subprocess.PIPE).stdout.read()
        subprocess.Popen("adb -s "+device+" shell am start com.github.gzuliyujiang.demo.oaid/com.github.gzuliyujiang.fallback.MainActivity ",shell=True, stdout=subprocess.PIPE).stdout.read()

        subprocess.Popen("adb -s "+device+" shell input tap 400 500 ",shell=True, stdout=subprocess.PIPE).stdout.read()
        time.sleep(1)
        res=subprocess.Popen("adb -s "+device+" shell cat /data/user/0/com.github.gzuliyujiang.demo.oaid/files/myfile.txt ",shell=True, stdout=subprocess.PIPE).stdout.read()

        print('msg=','No such file or directory' in res.decode('utf-8'))
        if('No such file or directory' in res.decode('utf-8')):
            print('No such file or directory yes ')
            time.sleep(1)
            res=subprocess.Popen("adb -s "+device+" shell cat /data/user/0/com.github.gzuliyujiang.demo.oaid/files/myfile.txt ",shell=True, stdout=subprocess.PIPE).stdout.read()
            if('No such file or directory' in res.decode('utf-8')):
                res="error"
                print('No such file or directory yes ')
                return None
        oaid=res
        subprocess.Popen("adb -s "+device+" shell am force-stop com.github.gzuliyujiang.demo.oaid ",shell=True, stdout=subprocess.PIPE).stdout.read()
        print(oaid)
        tp=subprocess.Popen("adb -s "+device+" shell getprop ro.product.model ",shell=True, stdout=subprocess.PIPE).stdout.read()
        #print('devinfo=',devinfo)
        self.sqlinsert(device,oaid,tp.decode('utf-8'))
        res=subprocess.Popen("adb -s "+device+" shell cat /data/user/0/com.example.myapplication/files/sample.txt ",shell=True, stdout=subprocess.PIPE).stdout.read()
        print('phone==',res.decode('utf-8'))
        self.sqlupdate(device,res.decode('utf-8'))


    def sqlcreate(self):
        conn = sqlite3.connect('E:\\data\\devinfo.db')

        conn.execute('''CREATE TABLE IF NOT EXISTS dev_table (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    number INTEGER,
                    devnum TEXT,
                    devinfo TEXT,
                    oaid TEXT,
                    type TEXT,
                    phone TEXT
                )''')

        conn.close()

        conn = sqlite3.connect('E:\\data\\moveinfo.db')

        conn.execute('''CREATE TABLE moveinfo (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    number INTEGER,
                    name TEXT,
                    address TEXT,
                    model TEXT,
                    resolution TEXT
                )''')

        conn.close()


    def sqlinsert(self,device, oaid, tp):
        conn = sqlite3.connect('E:\\data\\devinfo.db')

        # 写入数据
        conn.execute("INSERT INTO dev_table (devinfo, oaid, type) VALUES ( ?, ?,?)",
        (device, oaid, tp))

        # 提交更改
        conn.commit()
        # 关闭连接
        conn.close()

    def sqlupdate(self,devnum,phone):
        conn = sqlite3.connect('E:\\data\\devinfo.db')

        print('devnum========',devnum,'      phone===',phone)

        # 写入数据
        update_query = f"UPDATE dev_table SET phone = ? WHERE devinfo = ?"
        conn.execute(update_query, (phone, devnum))

        # 提交更改
        conn.commit()
        # 关闭连接
        conn.close()

    def sqlinsertInfo(self,number,address, model, resolution):
        conn = sqlite3.connect('E:\\data\\moveinfo.db')

        # 写入数据
        conn.execute("INSERT INTO moveinfo (number,address, model, resolution) VALUES (?, ?, ?, ?)",
        (number, address, model, resolution))

        # 提交更改
        conn.commit()
        # 关闭连接
        conn.close()

    def sqlselect(self,number,devnum):
        row=None
        conn = sqlite3.connect('E:\\data\\devinfo.db')
        # 读取数据
        cursor = conn.execute("SELECT * FROM dev_table where number="+number+" and devnum='"+devnum+"' LIMIT 1")
        for row in cursor:
            #print(f"id = {row[0]}, number = {row[1]}, devnum = {row[2]},devinfo = {row[3]}, oaid = {row[4]}, type = {row[5]}")
            print(f"id = {row[0]}, number = {row[1]},devnum = {row[2]},type = {row[5]},oaid = {row[4]}\n")
        # 关闭连接
        conn.close()
        if(row==None):
            return None
        return row[3]


if __name__ == '__main__':
    api=auto.API()
    change=change()
    start_time = time.time()

    #res=bak(10023)
    #res=change.random_devinfo('2-0')
    #res=change.transmit(10005,'7-2.txt')
    #res=change.transmitSql(10005,change.sqlselect('1-3'))
    #print(res)
    #res=api.devinfo('3-0')

    #change.oaid(api,1,'1-3','10005','1-0')

    #change.sqlcreate()
    #res=change.sqlselect('1-3')

    #print(res)
    end_time = time.time()

    # 计算并打印程序的运行时间
    elapsed_time = end_time - start_time
    print(f"程序运行时间：{elapsed_time} 秒")